
const express = require("express")
const router = express.Router()

const UserController = require("../../controllers/UserController")

// 处理图片上传的中间件
const multer = require("multer")
const upload = multer({ dest: 'public/uploads' })
/**
 * @api {get} /system/user 查询用户
 * @apiName find-user
 * @apiGroup User
 * @apiVersion 0.1.0
 * @apiParam  {Number} [limit] 页码.
 * @apiParam  {Number} [size] 条目数.
 * @apiParam  {String} [username] 用户名.
 * @apiParam  {String} [nickname] 昵称.
 * @apiParam  {Boolean} [status] 状态.
 * @apiParam  {string} [createTime] 创建时间.
 * @apiParam  {String} [telephone] 电话号码.
 * @apiParam  {String} [email] 邮箱.
 * @apiParam  {Number} [gender] 性别.
 * @apiParam  {String} [idnumber] 身份证号码.
 * @apiParam  {Array} [department] 部门.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 
 * {
 *      limit:1, // 页码
 *      size:10, // 数量
 *      username: "zhangs",
 *      nickname:"占山",
 *      status: true, // 是否禁用
 *      telephone: "18329935698",
 *      email: "yuezhanhope@163.com",
 *      idnumber: "610121199111151576",
 *      department: "",
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"用户查询成功",
 *       data:data,
 *       total:total
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"用户查询失败",
 *       data:null
 *   }
 */

/**
 * @api {post} /system/user 添加用户
 * @apiName append-user
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} username 用户名 必填项.
 * @apiParam  {String} [nickname] 用户昵称.
 * @apiParam  {String} password 用户密码 必填项.
 * @apiParam  {Boolean} status=true 状态 true为启动 false为禁用 必填项.
 * @apiParam  {String} [telephone] 电话号码.
 * @apiParam  {File} [avatar] 用户头像.
 * @apiParam  {String} [email] 邮箱.
 * @apiParam  {Number} [gender] 性别 0:女 1:男.
 * @apiParam  {String} [idnumber] 身份证号码.
 * @apiParam  {Array} [roles] 角色列表.
 * @apiParam  {Array} [department] 部门.
 * @apiParam  {Array} [post] 岗位.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 
 * {
 *      username: "zhangs",
 *      password: "123456",
 *      status: true,
 *      telephone: "18329935698",
 *      avatar:File,
 *      email: "yuezhanhope@163.com",
 *      idnumber: "610121199111151576",
 *      roles: ["角色ID"],
 *      department: "",
 *      post: [],
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"用户添加成功",
 *       data:null
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"用户添加失败",
 *       data:null
 *   }
 */
/**
 * @api {put} /system/user 修改用户
 * @apiName modify-user
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} _id 用户唯一标识 必填项.
 * @apiParam  {String} [nickname] 用户昵称.
 * @apiParam  {String} username 用户名 必填项.
 * @apiParam  {Boolean} status 状态 true为启动 false为禁用 必填项.
 * @apiParam  {String} [telephone] 电话号码.
 * @apiParam  {File} [avatar] 用户头像.
 * @apiParam  {String} [email] 邮箱.
 * @apiParam  {Number} [gender] 性别 0:女 1:男.
 * @apiParam  {String} [idnumber] 身份证号码.
 * @apiParam  {Array} [roles] 角色列表.
 * @apiParam  {Array} [department] 部门.
 * @apiParam  {Array} [post] 岗位.
 * @apiParam  {String} [type=undefine] 操作类型.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 
 * {
 *      username: "zhangs",
 *      status: true, // 是否禁用
 *      telephone: "18329935698",
 *      avatar:File,
 *      email: "yuezhanhope@163.com",
 *      idnumber: "610121199111151576",
 *      roles: ["角色ID"],
 *      department: "",
 *      post: [],
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"用户修改成功",
 *       data:null
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"用户修改失败",
 *       data:null
 *   }
 
 */
/**
 * @api {delete} /system/user/:id 删除用户
 * @apiName remove-user
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} _id 用户唯一标识 必填项.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"用户删除成功",
 *       data:null
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"用户删除失败",
 *       data:null
 *   }
 
 */
/**
 * @api {put} /system/user 修改用户头像
 * @apiName modify-userAvatar
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} _id 用户唯一标识 必填项.
 * @apiParam  {File} avatar 用户新上传的头像 必填项.
 * @apiParam  {String} type=avatar 操作类型.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 
 * {
 *      type:"avatar",
 *      _id: "ID",
 *      avatar:File
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"头像修改成功",
 *       data:avatar // 新的头像链接
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"头像修改失败",
 *       data:null
 *   }
 
 */
/**
 * @api {put} /system/user 修改用户密码
 * @apiName modify-userPassword
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} _id 用户唯一标识 必填项.
 * @apiParam  {string} oldPassword 旧密码 必填项.
 * @apiParam  {string} newPassword 新密码 必填项.
 * @apiParam  {String} type=password 操作类型.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 
 * {
 *      type:"password",
 *      _id: "ID",
 *      oldPassword:"123456",
 *      newPassword:"987654"
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"密码修改成功",
 *       data:null
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"密码修改失败",
 *       data:null
 *   }
 */
/**
 * @api {put} /system/user 修改用户排列顺序
 * @apiName modify-userSort
 * @apiGroup User
 * @apiVersion 0.1.0
 *
 * @apiParam  {String} _id 用户唯一标识 必填项.
 * @apiParam  {Number} sort 排序 必填项.
 * @apiParam  {String} type=sort 操作类型.
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} msg  提示消息.
 * @apiSuccess {Null} data  返回数据.
 *
 * @apiParamExample  {type} 请求示例:
 * 初始数据：[
 *      {_id:"2131456461534",sort:1,username:"zhangsan",...},
 *      {_id:"d123541115464",sort:2,username:"lisi",...},
 * ]
 * 上传的数据：
 * {
 *      type:"sort",
 *      data:[
 *               {_id:"2131456461534",sort:2,username:"zhangsan",...},
 *               {_id:"d123541115464",sort:1,username:"lisi",...},
 *           ]
 * }
 * 
 * @apiSuccessExample 成功返回结果:
 * 
 *  {
 *       code:"200",
 *       msg:"排序成功",
 *       data:null
 *  }
 *
 * @apiError {String} code 状态码.
 * @apiError {String} msg  提示消息.
 * @apiError {Null} data  返回数据.
 *
 * @apiErrorExample 错误返回结果:
 *   
 *   {
 *       code:"400",
 *       msg:"排序失败",
 *       data:null
 *   }
 */
router.get("/system/user", UserController.find)
router.post("/system/user", upload.single("avatar"), UserController.append)
router.put("/system/user", upload.single("avatar"), UserController.update)
router.delete("/system/user/:id", UserController.remove)

module.exports = router